#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""
import logging
import os
import sys
import json
import time

import requests
import multiprocessing

cur_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append("%s/.." % cur_dir)
import config

sys.path.append("%s/.." % cur_dir)
from res.lijiacai.utils import tool


class Inspection():

    def __init__(self):
        self.redis_client = tool.RedisDB(**config.redis_conf).client

    def run_(self):
        res = requests.get(config.dispatch_url).json()
        keys = {
            "重庆南区": ["九龙", "南坪", "巴南", "西永"],
            "重庆北区": ["北碚", "江北", "渝北", "鱼嘴"],
        }
        out = {}
        for i in res.get("data", []):
            for k, v in keys.items():
                if i.get("inspectionName") in v:
                    ids = out.get(k, [])
                    ids.append(i.get("stationId"))
                    out[k] = ids
        key = f"InspectionArea"
        try:
            self.redis_client.set(key, json.dumps(dict(out)), ex=None, px=None, nx=False, xx=False)
        except Exception as e:
            logging.exception(str(e))

    def go(self):
        res = self.run_()


if __name__ == '__main__':
    Inspection().go()
